/**
 *
 * Copyright 2012 TeleNav, Inc. All rights reserved.
 * SplashActivity.java
 * @author jwmeng (jwmeng@telenavsoftware.com)
 * @date 2012-10-17
 */
package com.telenav.app;

import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.packet.Presence;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.Handler;

import com.telenav.data.dao.AccountDao;
import com.telenav.data.dao.DaoManager;
import com.telenav.location.APPLocationManager;
import com.telenav.persistent.PersistentContext;
import com.telenav.persistent.PersistentManager;
import com.telenav.serverproxy.ICityServerProxy;
import com.telenav.serverproxy.ServerProxyFactory;
import com.telenav.threadpool.Notifier;
import com.telenav.xmpp.XmppTool;

public class SplashActivity extends Activity {

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.splash);

		new Thread(new Runnable() {
			@Override
			public void run() {
				while (!Thread.currentThread().isInterrupted()) {
					try {
						init();
						Thread.sleep(1000);
					} catch (InterruptedException e) {
						Thread.currentThread().interrupt();
					}
				}
			}
		}).start();

		new Handler().postDelayed(new Runnable() {
			public void run() {
				Intent i = new Intent(SplashActivity.this, HomeActivity.class);
				SplashActivity.this.startActivity(i);
				SplashActivity.this.finish();
			}
		}, 3000);
	}

	private void init() {
		PersistentContext.getInstance().init(this);

		if (PersistentContext.getInstance().getApplicationSQLiteDatabase() == null) {
			PersistentContext.getInstance().openSQLiteDatabase();
		}

		// bellow are module init.
		
		initLocation();

		initPersistent();
		
		Notifier.getInstance().start();
		
		// some other manager, for example mvc & notification.
		CommManager.getInstance().initializeHosts();
		
		login();
				
		reverseCityGeocode();
	}

	public void initPersistent() {
		if (PersistentManager.getInstance() == null) {
			PersistentManager.init(new PersistentManager(),
					PersistentContext.getInstance());
		}
	}

	public void initLocation() {
		if (APPLocationManager.getInstance() == null) {
			LocationManager lm = (LocationManager) PersistentContext
					.getInstance().getContext()
					.getSystemService(Context.LOCATION_SERVICE);
			APPLocationManager.init(new APPLocationManager(
					LocationManager.NETWORK_PROVIDER, lm));
		}
	}

	public void reverseCityGeocode() {
		//Location loc = APPLocationManager.getInstance().getLastKnownLocation();
		
		/*if (loc == null)
			return;
		*/
		ICityServerProxy createCityProxy = ServerProxyFactory.getInstance().createCityProxy(null, CommManager.getInstance().getComm(), null);
		createCityProxy.reverseCityGeocode(39.983424, 116.322987);
	}

	private void login() {

		if(AccountDao.getInstance().isLogin() && !XmppTool.getConnection().isAuthenticated())
		{
			String username = DaoManager.getInstance().getAccountDao().getAccountName();
			String password = DaoManager.getInstance().getAccountDao().getAccountPWD();
			
			try {
				XmppTool.getConnection().login(username, password);
			} catch (XMPPException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
    		
			Presence presence = new Presence(Presence.Type.available);
			XmppTool.getConnection().sendPacket(presence);
		}
	}

	public void onDestroy() {
		super.onDestroy();
		try {
			PersistentContext.getInstance().closeSQLiteDatabase();
		} catch (Throwable e) {
		}
	}
}